package org.wmq.travelagency02.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.model.Entity.TourGroup;
import org.wmq.travelagency02.service.TourGroupService;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/tour-group")
@RequiredArgsConstructor
public class TourGroupController {
    @Autowired
    private TourGroupService tourGroupService;

    @GetMapping("/{id}")
    public Result<TourGroup> getById(@PathVariable Long id){
        TourGroup result = tourGroupService.getById(id);
        if (result == null){
            return Result.error("没有找到该数据");
        }
        return Result.ok(result);
    }

    @GetMapping("/page/{pageNo}/{pageSize}")
    public Result<Page<TourGroup>> getPage(@PathVariable Integer pageNo, @PathVariable Integer pageSize){
        Page<TourGroup> result = this.tourGroupService.page(new Page<TourGroup>(pageNo, pageSize));
        return Result.ok(result);
    }

    @PostMapping
    public Result<Void> save(@RequestBody TourGroup tourGroup){
        tourGroup.setCreateTime(LocalDateTime.now());
        tourGroup.setUpdateTime(LocalDateTime.now());
        if (tourGroupService.save(tourGroup)){
            return Result.ok();
        }
        return Result.error("保存失败");
    }

    @PutMapping
    public Result<Void> update(@RequestBody TourGroup tourGroup){
        tourGroup.setUpdateTime(LocalDateTime.now());
        if (tourGroupService.updateById(tourGroup)){
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id){
        if (tourGroupService.removeById(id)){
            return Result.ok();
        }
        return Result.error("删除失败");
    }
}
